package com.swmutil.swmutil.sqlite;

/**
 * @author 师文明
 * @data 2024/10/21 下午5:22
 **/

import com.swmutil.swmutil.pojo.All_Param;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author cpf
 * @Date 2023/9/2
 */

public class AllParamSql {

    //TODO 查询所有数据
    private static final String SQL_queryList = "SELECT * FROM all_param;";
    private static final String SQL_queryListByPage = "SELECT * FROM all_param where page_id = ";
    //TODO 新增数据
    private static final String SQL_saveList = "INSERT INTO all_param (signal_name, signal_description, bit_length) VALUES (";
    private static final String SQL_update = "update  all_param set value = ";
    //TODO 根据ID删除数据
    private static final String SQL_deleteList = "DELETE FROM all_param WHERE id = ";

    //TODO 查询
    public static List<All_Param> queryParamsList(){
        List<All_Param> params = new ArrayList<All_Param>();
        try {
            ResultSet rs = ProjectStaticManager.statement.executeQuery(SQL_queryList);
            while (rs.next()) {
                All_Param param = new All_Param(rs.getInt("id"), rs.getString("signal_name"), rs.getString("signal_description"), rs.getInt("bit_length"), rs.getInt("param_order"), rs.getInt("value"), rs.getInt("layoutx"), rs.getInt("layouty"),rs.getString("options"), rs.getInt("type"),rs.getString("unit"), rs.getInt("page_id"));
                params.add(param);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return params;
    }
    public static List<All_Param> queryParamsListByPage(Integer pageid) {
        List<All_Param> params = new ArrayList<All_Param>();
        try {
            ResultSet rs = ProjectStaticManager.statement.executeQuery(SQL_queryListByPage + pageid + " order  by bit_length;");
            while (rs.next()) {
                All_Param param = new All_Param(rs.getInt("id"), rs.getString("signal_name"), rs.getString("signal_description"), rs.getInt("bit_length"), rs.getInt("param_order"), rs.getInt("value"), rs.getInt("layoutx"), rs.getInt("layouty"),rs.getString("options"), rs.getInt("type"),rs.getString("unit"), rs.getInt("page_id"));
                params.add(param);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return params;
    }

    public static int updateParam(Integer value,Integer id)   {
        try {
            return ProjectStaticManager.statement.executeUpdate(SQL_update + value + " where id="+id+";");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    //TODO 根据ID删除数据
    public static int deleteImgSizeById(String id) {
        int num = 0;
        try {
            num = ProjectStaticManager.statement.executeUpdate(SQL_deleteList + id + ";");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }

    //TODO 新增
   /* public static int saveImgSize(HgBqImgSize bq){
        int num = 0;
        try {
            num = ProjectStaticManager.statement.executeUpdate(SQL_saveList +
                    bq.getCardType() + ","+
                    bq.getCutOrder() +","+
                    bq.getPointX() + ","+
                    bq.getPointY() + ","+
                    bq.getWidth() + ","+
                    bq.getHeight() +
                    ")"+ ";");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }*/

    public static void insertPage(All_Param param) throws SQLException {
        // 插入数据 id是自增，因此新增的时候可以不管id
        String insertParamSQL = "INSERT INTO all_param (signal_name, signal_description, bit_length,param_order,value,layoutx,layouty,type,options,unit,page_id) VALUES ('" + param.getSignal_name() + "', '" + param.getSignal_description() + "', " + param.getBit_length() + "," + param.getParam_order() + "," + param.getValue() + "," + param.getLayoutx() + "," + param.getLayouty() + "," + param.getType() + ",'" + param.getOptions()+"','" + param.getUnit() +"'," + param.getPage_id() + ")";
        ProjectStaticManager.statement.executeUpdate(insertParamSQL);

    }

    public static void main(String[] args) {
        try {
            insertPage(new All_Param());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

